/* 
 * (C) 2013 Daniel Garcia <contacto {at} danigarcia.org>
 * 
 * http://danigarcia.org
 * 
 * This file is part of BlackList.
 * 
 * BlackList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BlackList is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BlackList.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package org.danigarcia.android.blacklist;

import org.danigarcia.android.blacklist.database.ConfigManager;
import org.danigarcia.android.blacklist.database.DataManager;
import org.danigarcia.android.blacklist.database.SmsListCursorAdapter;
import org.danigarcia.android.blacklist.service.BlockingService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends BaseActivity 
		implements OnClickListener {

	private static final String TAG = MainActivity.class.getName();

	private DataManager			dm;
	private boolean				status	= false;
	private boolean				pickUp	= false;
	private boolean				service	= false;
	private boolean				saveSms	= false;
	private boolean				filterSms	= false;
	private boolean				filterSmsBody	= false;
	private boolean				showNotifications = false;
	
	// UI elements
	private ToggleButton 	btnStatus;
	private ToggleButton 	btnService;
	private ToggleButton 	btnPickup;
	private ToggleButton	btnSmsFiltering;
	private ToggleButton	btnSmsBodyFiltering;
	private ToggleButton	btnSaveSms;
	private ToggleButton	btnShowNotifications;
	
	private Button 			btnExit;
	//private ImageButton		btnConfig;
	
	private TextView		lblRegisteredNumbers;
	private TextView		lblBlockedCalls;
	private TextView		lblBlockedSms;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		configureActivity();
		loadSettings();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId()) {
        case R.id.menuMainConfigNumbers:
        	startBlockedNumbersActivity(null);
            return true;
            
        case R.id.menuMainListLoadCalls:
        	startBlockedCallsActivity(null);
            return true;
            
        case R.id.menuMainListLoadSms:
        	startSmsActivity();
            return true;
            
        case R.id.menuMainAbout:
        	about();
            return true;
            
        default:
            return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	public void onDestroy() {
		if(ConfigManager.getBoolean(ConfigManager.KEY_STATUS))
			toast(getString(R.string.msgServiceForeground));
		this.dm = null;
		ConfigManager.dispose();
		super.onDestroy();
	}
	
	public void onResume() {
		super.onResume();
		loadSettings();
	}
	
	@Override
	public void onClick(View v) 
	{
		switch(v.getId())
		{
			case R.id.btnService:
			{
				activateService(btnService.isChecked());
				break;
			}
			case R.id.btnStatus:
			{
				turnOn(btnStatus.isChecked());
				break;
			}
			case R.id.btnPickup:
			{
				pickUpCalls(btnPickup.isChecked());
				break;
			}
			case R.id.btnSmsFiltering:
			{
				filterSms(btnSmsFiltering.isChecked());
				break;
			}
			case R.id.btnSmsBodyFiltering:
			{
				filterSmsBody(btnSmsBodyFiltering.isChecked());
				break;
			}
			case R.id.btnSaveSms:
			{
				saveSms(btnSaveSms.isChecked());
				break;
			}
			case R.id.btnShowNotifications:
			{
				showNotifications(btnShowNotifications.isChecked());
				break;
			}
			case R.id.btnExit:
			{
				AlertDialog dialog = createConfirmDialog(getString(R.string.confirm), getString(R.string.msgConfirmExit), null);
				dialog.show();
				break;
			}
			default:
				break;
		}
	}
	
	@Override
	protected void configureActivity()
	{
		btnStatus				= (ToggleButton)findViewById(R.id.btnStatus);
		btnService				= (ToggleButton)findViewById(R.id.btnService);
		btnPickup				= (ToggleButton)findViewById(R.id.btnPickup);
		btnSmsFiltering			= (ToggleButton)findViewById(R.id.btnSmsFiltering);
		btnSmsBodyFiltering		= (ToggleButton)findViewById(R.id.btnSmsBodyFiltering);
		btnSaveSms				= (ToggleButton)findViewById(R.id.btnSaveSms);
		btnShowNotifications	= (ToggleButton)findViewById(R.id.btnShowNotifications);
		
		//btnConfig				= (ImageButton)findViewById(R.id.btnConfig);
		btnExit					= (Button)findViewById(R.id.btnExit);
		
		lblRegisteredNumbers	= (TextView)findViewById(R.id.lblRegisteredNumbers);
		lblBlockedCalls			= (TextView)findViewById(R.id.lblBlockedCalls);
		lblBlockedSms			= (TextView)findViewById(R.id.lblBlockedSms);
		
		
		btnStatus.setOnClickListener(this);
		btnService.setOnClickListener(this);
		btnPickup.setOnClickListener(this);
		btnSmsFiltering.setOnClickListener(this);
		btnSmsBodyFiltering.setOnClickListener(this);
		btnSaveSms.setOnClickListener(this);
		btnShowNotifications.setOnClickListener(this);
		
		//btnConfig.setOnClickListener(this);
		btnExit.setOnClickListener(this);
		
		ConfigManager.init(getApplicationContext());
		
		//preferences = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
		dm = new DataManager(this, DataManager.DB_NAME, null, DataManager.DB_VERSION);
	}
	
	@Override
	protected CursorAdapter getAdapter(String filter, String orderBy) {
		return null;
	}
	
	private void loadSettings()
	{
		status = ConfigManager.getBoolean(ConfigManager.KEY_STATUS);
		service = ConfigManager.getBoolean(ConfigManager.KEY_SERVICE);
		pickUp = ConfigManager.getBoolean(ConfigManager.KEY_PICKUP);
		filterSms = ConfigManager.getBoolean(ConfigManager.KEY_FILTER_SMS);
		filterSmsBody = ConfigManager.getBoolean(ConfigManager.KEY_FILTER_SMS_BODY);
		saveSms = ConfigManager.getBoolean(ConfigManager.KEY_SAVE_SMS);
		showNotifications = ConfigManager.getBoolean(ConfigManager.KEY_SHOW_NOTIFICATIONS);
		
		btnStatus.setChecked(status);
		btnPickup.setChecked(pickUp);
		btnService.setChecked(service);
		btnSmsFiltering.setChecked(filterSms);
		btnSmsBodyFiltering.setChecked(filterSmsBody);
		btnSaveSms.setChecked(saveSms);
		btnShowNotifications.setChecked(showNotifications);
		
		lblRegisteredNumbers.setText(String.valueOf(dm.getTotalNumbers()));
		lblBlockedCalls.setText(String.valueOf(dm.getTotalCalls(null)));
		lblBlockedSms.setText(String.valueOf(dm.getTotalSms(null)));
		
	}
	
	private void activateService(boolean activate)
	{
		this.service = activate;
		ConfigManager.insert(ConfigManager.KEY_SERVICE, this.service);
	}
	
	private void turnOn(boolean activate)
	{
		this.status = activate;
		ConfigManager.insert(ConfigManager.KEY_STATUS, this.status);
		Intent intent = new Intent(getApplicationContext(), BlockingService.class);
		
		if(this.status)
		{
			Log.d(TAG, "turnOn(true)");
			startService(intent);
		}
		else
		{
			Log.d(TAG, "turnOn(false)");
			stopService(intent);
		}
	}
	
	private void pickUpCalls(boolean pick)
	{
		this.pickUp = pick;
		ConfigManager.insert(ConfigManager.KEY_PICKUP, this.pickUp);
	}
	
	private void filterSms(boolean filter)
	{
		this.filterSms = filter;
		ConfigManager.insert(ConfigManager.KEY_FILTER_SMS, this.filterSms);
	}
	
	private void filterSmsBody(boolean filter)
	{
		this.filterSmsBody = filter;
		ConfigManager.insert(ConfigManager.KEY_FILTER_SMS_BODY, this.filterSmsBody);
	}
	
	private void saveSms(boolean save)
	{
		this.saveSms = save;
		ConfigManager.insert(ConfigManager.KEY_SAVE_SMS, this.saveSms);
	}
	
	private void showNotifications(boolean show)
	{
		this.showNotifications = show;
		ConfigManager.insert(ConfigManager.KEY_SHOW_NOTIFICATIONS, this.showNotifications);
	}
	
	private void startBlockedNumbersActivity(String filter)
	{
		Intent intent = new Intent(this, BlockedNumbersActivity.class);
		intent.putExtra(BlockedNumbersActivity.PARAMETER_FILTER, filter);
		startActivityForResult(intent, -1);
	}
	
	private void startSmsActivity()
	{
		Intent intent = new Intent(this, BlockedSmsActivity.class);
		startActivityForResult(intent, -1);
	}
	
	private void startBlockedCallsActivity(String filter)
	{
		Intent intent = new Intent(this, BlockedCallsActivity.class);
		if(filter != null)
			intent.putExtra(BlockedNumbersActivity.PARAMETER_FILTER, filter);
		startActivityForResult(intent, -1);
	}

}
